package com.talent.study.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;

/**
 * @Author: hszhou
 * @Date: 2025/4/25 16:02
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
public class Client {

    public static void main(String[] args) throws InterruptedException {


        NioEventLoopGroup work = new NioEventLoopGroup();

        Bootstrap bootstrap= new Bootstrap();
        bootstrap.group(work)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {

                        ChannelPipeline pipeline = ch.pipeline();
                        ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
                       // pipeline.addLast(new LineBasedFrameDecoder(65535));
                        pipeline.addLast(new DelimiterBasedFrameDecoder(65535, delimiter));
                        pipeline.addLast(new ClientHandler());
                    }
                });

        ChannelFuture sync = bootstrap.connect("127.0.0.1",8080).sync();

        sync.channel().closeFuture().addListener(future -> {


            work.shutdownGracefully();
        });
    }
}
